package com.neusoft.bizcore.auth.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.auth.dto.DingConfigDTO;
import com.neusoft.bizcore.auth.service.DingDingService;
import com.neusoft.bizcore.web.dto.result.ResultDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("钉钉对接管理")
@RestController
@RequestMapping(value = "/api/w/auth/dingding")
public class DingDingController {

    @Autowired
    private DingDingService dingDingService;

    @ApiOperation("获取钉钉应用配置信息")
    @GetMapping
    public ResultDTO<?> getConfig() {
        final DingConfigDTO dto = this.dingDingService.getDingConfig();
        if (null == dto) {
            return ResultDTO.failure();
        }
        return ResultDTO.success(dto);
    }

    @ApiOperation("创建钉钉应用配置信息")
    @PostMapping
    public ResultDTO<DingConfigDTO> create(@RequestBody final DingConfigDTO dto) {
        final DingConfigDTO result = this.dingDingService.setDingConfig(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "修改钉钉应用配置信息")
    @PutMapping("/{id}")
    public ResultDTO<DingConfigDTO> update(@PathVariable final Long id,
            @RequestBody final DingConfigDTO dto) {
        dto.setId(id);
        final DingConfigDTO result = this.dingDingService.setDingConfig(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("删除钉钉应用配置信息")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.dingDingService.delete(id);
        return ResultDTO.success();
    }

    /**
     * https://ding-doc.dingtalk.com/document#/org-dev-guide/logon-free-process
     *
     * @param authCode
     * @return
     */
    @PostMapping("/login")
    public ResultDTO<UserBean> login(@RequestParam final Map<String, String> params) {
        final String authCode = params.get("authCode");
        final String username = params.get("username");
        final String password = params.get("password");
        final UserBean userBean = this.dingDingService.login(authCode, username, password);
        return ResultDTO.success(userBean);
    }

    @ApiOperation("根据系统用户获取钉钉用户")
    @GetMapping("/systemuser/{username}/exchange")
    public ResultDTO<?> exchangeDingUser(@PathVariable final String username) {
        final String dingUserId = this.dingDingService.getDingUserIdBySystemUser(username);
        return ResultDTO.success(dingUserId);
    }

}
